var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = e(require("../../@babel/runtime/regenerator")), s = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), i = require("../../@babel/runtime/helpers/asyncToGenerator"), a = require("../../utils/tool"), r = e(require("../../http/address")), d = e(require("../../http/order")), o = new r.default(), n = new d.default();

Component({
    properties: {
        selectList: {
            type: Array
        }
    },
    data: {
        show: !1,
        couponHistoryId: "",
        couponList: [],
        addressFlag: !1,
        address: {},
        addressList: [],
        addressId: "",
        count: "",
        price: "",
        expressList: [],
        expressId: "",
        isfree: 0,
        submitType: 2
    },
    lifetimes: {
        attached: function() {}
    },
    methods: {
        couponOnSelected: function(e) {
            e.detail.couponHistoryId ? this.setData({
                submitType: 1,
                couponHistoryId: e.detail.couponHistoryId,
                expressTypeText: 0 == e.detail.expressType ? "普通快递包邮" : "顺丰快递包邮",
                expressId: ""
            }) : this.setData({
                submitType: this.data.isfree ? 0 : 2,
                expressId: this.data.expressList[0].shippingTemplatePriceId,
                couponHistoryId: ""
            });
        },
        computePrice: function() {
            var e = this;
            return i(t.default.mark(function i() {
                var r, d, o, c;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.next = 2, n.pickOrderPrice({
                            addressId: e.data.address.addressId,
                            omsOrderList: e.properties.selectList
                        });

                      case 2:
                        if ((r = t.sent).data.couponHistoryList) {
                            d = s(r.data.couponHistoryList);
                            try {
                                for (d.s(); !(o = d.n()).done; ) (c = o.value).isUsable = (0, a.isLessThanNow)(c.collectStartTime), 
                                c.isExpire = (0, a.getDifferentDays)(c.collectEndTime) < 7;
                            } catch (e) {
                                d.e(e);
                            } finally {
                                d.f();
                            }
                        }
                        r.data.mergeOrderStatisticsVo.freeShippingStatus && r.data.shippingPriceList.map(function(e) {
                            "common" == e.expressCompanyCode && (e.expressCompanyName = "普通快递包邮", e.shippingMoney = 0);
                        }), e.setData({
                            count: r.data.mergeOrderStatisticsVo.quantityTotal,
                            price: r.data.mergeOrderStatisticsVo.paymentTotal,
                            expressList: r.data.shippingPriceList,
                            expressId: r.data.shippingPriceList[0].shippingTemplatePriceId,
                            isfree: r.data.mergeOrderStatisticsVo.freeShippingStatus,
                            submitType: r.data.mergeOrderStatisticsVo.freeShippingStatus ? 0 : 2,
                            couponList: r.data.couponHistoryList || [],
                            expressTypeText: r.data.mergeOrderStatisticsVo.freeShippingStatus ? "普通快递包邮" : null
                        }), e.showPopup(), wx.hideLoading();

                      case 8:
                      case "end":
                        return t.stop();
                    }
                }, i);
            }))();
        },
        expressOnChange: function(e) {
            var t = e.currentTarget.dataset.item;
            this.setData({
                expressId: t.shippingTemplatePriceId,
                submitType: this.data.isfree && 0 == t.shippingMoney ? 0 : 2
            }), console.log(this.data.expressId);
        },
        getDefaultAddress: function() {
            var e = this;
            wx.showLoading({
                title: "加载中",
                mask: !0
            }), o.getDefaultAddress().then(function(t) {
                e.setData({
                    address: t.data
                }), e.computePrice();
            }).catch(function(e) {
                wx.hideLoading();
            });
        },
        getAddressList: function() {
            var e = this;
            return i(t.default.mark(function s() {
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        o.addressList().then(function(t) {
                            e.setData({
                                addressList: t.data.list
                            });
                        });

                      case 1:
                      case "end":
                        return t.stop();
                    }
                }, s);
            }))();
        },
        selectAddress: function(e) {
            var t = e.currentTarget.dataset.id, s = this.data.addressList.findIndex(function(e) {
                return e.addressId == t;
            });
            this.setData({
                addressId: t,
                address: this.data.addressList[s],
                couponHistoryId: ""
            }), this.hideAddress(), this.computePrice();
        },
        sureDeliver: function() {
            var e = this;
            wx.showLoading({
                title: "加载中",
                mask: !0
            }), n.addPick({
                addressId: this.data.address.addressId,
                orderIds: this.properties.selectList.join(","),
                shippingPriceTemplateId: this.data.expressId,
                submitType: this.data.submitType,
                couponHistoryId: this.data.couponHistoryId
            }).then(function(t) {
                2 == e.data.submitType ? e.getCode(t.data.mergeOrderId) : (wx.hideLoading(), e.onClose(), 
                (0, a.toastToPage)("申请发货成功"), e.triggerEvent("refresh", {}));
            }).catch(function(e) {
                wx.hideLoading();
            });
        },
        getCode: function(e) {
            var t = this;
            wx.login({
                success: function(s) {
                    s.code ? t.getPayInfo(s.code, e) : ((0, a.toastToPage)("获取code失败" + s.errMsg), wx.hideLoading());
                }
            });
        },
        getPayInfo: function(e, t) {
            var s = this;
            n.getPostWxPayInfo(e, t).then(function(e) {
                (0, a.callWeChatPay)(e.data, function(e) {
                    wx.hideLoading(), (0, a.toastToPage)("申请发货成功"), s.onClose(), s.triggerEvent("refresh", {});
                }, function(e) {
                    s.cancelOrder(t);
                });
            }).catch(function(e) {
                (0, a.toastToPage)(e.msg), wx.hideLoading();
            });
        },
        cancelOrder: function(e) {
            n.cancelPick(e).then(function(e) {
                wx.hideLoading(), (0, a.toastToPage)("订单已取消");
            }).catch(function(e) {
                (0, a.toastToPage)(e.msg), wx.hideLoading();
            });
        },
        toEditAddress: function(e) {
            var t = e.currentTarget.dataset.id;
            wx.navigateTo({
                url: "/pages/edit_address/edit_address?addressid=".concat(t)
            });
        },
        onClose: function() {
            this.setData({
                show: !1,
                couponHistoryId: ""
            });
        },
        showPopup: function() {
            this.setData({
                show: !0
            });
        },
        showAddress: function() {
            this.getAddressList(), this.setData({
                addressFlag: !0
            });
        },
        hideAddress: function() {
            this.setData({
                addressFlag: !1
            });
        },
        showTicket: function() {
            0 != this.data.couponList.length && this.selectComponent("#ticket").showPopup();
        },
        toAddress: function() {
            wx.navigateTo({
                url: "/pages/address/address"
            });
        }
    }
});